<?php

namespace EIP\C;
require_once('ControllerBase.php');
//our authenication module
require_once('modules/Authenication/EIP_Authenication.php');

use \EIP\EIP_Authenication as EIP_Authenication;

class Controller extends ControllerBase {
	public function __construct($full_name,$action)
	{
		global $action;
		parent::__construct($full_name,$action);
		
		$this->logger_ide = \E::get_logger(get_class($this).'_ide');
		
		$this->layout[0][] = 'EIP/editor_div.tpl';
		$this->eip_urls=array();
		
		
		$this->scripts[] = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js";
		$this->styles[] = \E::get_raw_url('resources/css/EIP/common.css');
		//add a hook for pre content stuff. If it is a static page, display the edit toolbar
		$this->pre_header_callbacks[] = function( $c ) {
			if($c->isStaticPage())
			{
				$c->logger_ide->debug("Including EIP Toolbar");
				array_push($c->eip_urls,\E::get_url('Ide/Toolbar'));
			}
		};
		$this->smarty->assignByRef('eip_urls',$this->eip_urls);
		\E::loadRedBean(); //make sure we load redbean for misc tasks

		//require logins for all pages in this mode
		
        $this->auth_module = new EIP_Authenication('eip_admin_');
		$this->logger_ide->info("Redirecting user to login @ '/EIP/login?sc={$this->full_name}&sa={$action}'");
		
		if( ($this->full_name != 'EIP/Index' && $this->action!='login') && ( !$this->auth_module->isLoggedIn() || !$this->auth_module->hasAccess('admin-base','','*') ) )
		{
			$this->logger_ide->info("Redirecting user to login @ '/EIP/login?sc={$this->full_name}&sa={$action}'");
			//user is not logged in or does not have access to admin functions
			//redirect them to login page
			header('Location: '.\E::get_url('EIP','login')."?sc={$this->full_name}&sa={$action}");
			exit;
		} 
	
	}
	
	public function __call($method, $args) {
		try {
			parent::__call($method, $args);
			
		} catch(ExceptionActionNotFound $e) {
			$this->logger_ide->error("Could not find '".get_class($this)."'->'{$method}'. Showing 'action not found' template.");
			$this->base_template_name = 'EIP/action_not_found.tpl';	
		}

	}
	
}



?>
